package com.example.businesscalender;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import com.example.database.DataBaseAdapter;

public class RetrieveEvent extends ListActivity {

    private DataBaseAdapter dbHelper;
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private Cursor cursor;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.retriveevent);
        this.getListView().setDividerHeight(2);
        dbHelper = new DataBaseAdapter(RetrieveEvent.this);
        dbHelper.open();
        fillData();
        registerForContextMenu(getListView());
    }

    // Create the menu based on the XML defintion
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.listmenu, menu);
        return true;
    }

    // Reaction to the menu selection
   @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch (item.getItemId()) {
        case R.id.save:
            createTodo();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.save:
            createTodo();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            dbHelper.deleteEvent(info.id);
            fillData();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    private void createTodo() {

        Intent i = new Intent(this, AddEvent.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    // ListView and view (row) on which was clicked, position and
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, AddEvent.class);
        i.putExtra(DataBaseAdapter.KEY_ROWID, id);
        // Activity returns an result if called with startActivityForResult

        startActivityForResult(i, ACTIVITY_EDIT);
    }

    // Called with the result of the other activity
    // requestCode was the origin request code send to the activity
    // resultCode is the return code, 0 is everything is ok
    // intend can be use to get some data from the caller
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        super.onActivityResult(requestCode, resultCode, intent);
        fillData();

    }

    private void fillData() {

        cursor = dbHelper.fetchAllEvents();
        startManagingCursor(cursor);

        String[] from = new String[] {
            DataBaseAdapter.KEY_ABOUT
        };
        int[] to = new int[] {
            R.id.label
        };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.event_row, cursor, from, to);
        setListAdapter(notes);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);
        //menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        if (dbHelper != null) {
            dbHelper.close();
            finish();
        }
    }
}
